home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / game / think / C4.lha / C4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-29  |  22.2 KB  |  1,047 lines

  1. /*
  2.           C4 Copyright © 1992 Danny Y. Wong  All rights reserved.
  3.  
  4. Version 1.0  Released October 28, 1992
  5.  
  6. This program is in the Public Domain; you have permission to 
  7. redistribute it and/or modify it under the terms in the GNU
  8. General Public License.  However, this program can not be sold except
  9. for the cost of the media.
  10.  
  11. Any comments can be sent to:
  12.  
  13. Danny Y. Wong
  14. 131 64 Ave NW
  15. Calgary, Alberta
  16. T2K 0L9
  17. CANADA
  18.  
  19. --------------------------------------------------------------------------
  20.  
  21. To compile in SAS/C:   lc -L c4
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include "exec/types.h"
  27. #include "intuition/intuition.h"
  28. #include "graphics/gfxbase.h"
  29.  
  30. #define VERSION "C4 V1.0  Copyright © 1992 By Danny Y. Wong."
  31. #define DEPTH   3      // spare page
  32. #define WIDTH   320
  33. #define HEIGHT  200
  34.  
  35. #define FWIDTH  320  // foreground
  36. #define FHEIGHT 400
  37.  
  38. #define BWIDTH  320  // background
  39. #define BHEIGHT 400
  40.  
  41. #define VPWIDTH  320
  42. #define VPHEIGHT 200
  43.  
  44. #define JOY1DAT 0xDFF00C
  45. #define PA0 0xBFE0FF
  46.  
  47. #define NOT_ENOUGH_MEMORY -1000
  48. #define RED  1
  49. #define BLUE 2
  50. #define TIE  3
  51. #define CROSS 1
  52. #define DOWN  2
  53. #define RDIAGONAL 3
  54. #define LDIAGONAL 4
  55.  
  56. void remakeView(), swapPointers(), scrollit(), FreeMemory(), readthepic();
  57. void setupjoystick(), readjoystick(), readthepic();
  58. short do_menu(), get_box(), drop_peg(), check_win(), check_cross_win();
  59. short comp_check_cross(), comp_check_down();
  60. void move_peg(), game_loop(), read_qstick(), do_init_game();
  61. void do_new_game(), do_about(), do_game_over();
  62. void print_board(), do_random_move();
  63. short do_block_player(), check_comp_rdiagonal(), check_comp_ldiagonal();
  64.  
  65. struct View     v;
  66. struct ViewPort vp;
  67. struct ColorMap *cm;   /* pointer to colormap structure, dynamic alloc */
  68.  
  69. struct BitMap   b,  b2, b3;
  70. struct RastPort rp, rp2;
  71. struct RasInfo  ri, ri2;
  72.  
  73. struct GfxBase *GfxBase;
  74. struct View *oldview;      /* save pointer to old view so can restore */
  75.  
  76. UWORD colortable[] =  { 
  77.     0x000, 0xbbb, 0x999, 0x777,  //foreground
  78.    0x24c, 0xeb0, 0xf10, 0x0bb,
  79.  
  80.     0x000, 0xbbb, 0x999, 0x777,
  81.    0x24c, 0xeb0, 0xf10, 0x0bb,
  82.     };
  83.  
  84. UWORD  *colorpalette;
  85. struct cprlist *LOF[2];
  86. struct cprlist *SHF[2];
  87.  
  88. short w;
  89. char joy_fire,button_status,*button_ptr;
  90. short joy_status,joy_up,joy_down,joy_left,joy_right,*joy_ptr;
  91. char *file;
  92. short Redx=13, Redy=156, Bluex=37, Bluey=156, page=0, offsety=0,
  93.       drop[9]={12, 37, 62, 87, 112, 137, 162, 187, 212},
  94.       dropy[6]={26, 45 , 69, 93 ,116 ,140};
  95. short board[8][11], total_pegs, game=1;
  96.  
  97. struct comp_turn
  98. {
  99.    short firstx, firsty, lastx, lasty;
  100. }Comp_stuff;
  101.  
  102. void _main()
  103. {
  104.    short i;
  105.    
  106.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  107.     if (GfxBase == NULL) exit(1);
  108.     oldview = GfxBase->ActiView; /* save current view to restore later */
  109.     /* example steals screen from Intuition if started from WBench */
  110.  
  111.     /* init bitmap structures */
  112.     InitBitMap(&b,DEPTH,FWIDTH,FHEIGHT);
  113.     InitBitMap(&b2,DEPTH,BWIDTH,BHEIGHT);
  114.     InitBitMap(&b3,DEPTH,WIDTH,HEIGHT);
  115.  
  116.     /* allocate and clear bitplanes */
  117.     for(i=0; i<DEPTH; i++)
  118.     {
  119.         b.Planes[i] = (PLANEPTR)AllocRaster(FWIDTH,FHEIGHT);  // foreground
  120.         if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  121.         BltClear(b.Planes[i],RASSIZE(FWIDTH,FHEIGHT),0);
  122.  
  123.         b3.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT); //spare
  124.         if(b3.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  125.         BltClear(b3.Planes[i],RASSIZE(WIDTH,HEIGHT),0);
  126.  
  127.         b2.Planes[i] = (PLANEPTR)AllocRaster(BWIDTH,BHEIGHT); //big
  128.         if(b2.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  129.         BltClear(b2.Planes[i],RASSIZE(BWIDTH,BHEIGHT),0);
  130.     }
  131.  
  132.     /* init RasInfo structures */
  133.     ri.BitMap   = &b;
  134.     ri.RxOffset = 0;   
  135.     ri.RyOffset = 0;
  136.     ri.Next = &ri2;
  137.  
  138.     ri2.BitMap = &b2;
  139.     ri2.RxOffset = 0;
  140.     ri2.RyOffset = 0;
  141.     ri2.Next = 0;
  142.  
  143.     InitView(&v);        /* initialize view */
  144.     v.ViewPort = &vp;    /* link view into viewport */
  145.  
  146.     InitVPort(&vp);      /* init view port */
  147.  
  148.     /* First color reg for 2nd playfield is register 8 */
  149.     cm = (struct ColorMap * )GetColorMap(16);
  150.     colorpalette = (UWORD *)cm->ColorTable;
  151.     for(i=0; i<16; i++)
  152.         *colorpalette++ = colortable[i];
  153.     /* copy my colors into this data structure */
  154.     vp.ColorMap = cm;   /* link it with the viewport */
  155.  
  156.     /* now specify critical characteristics */
  157.     vp.DWidth  = VPWIDTH;   /* this is how much you will SEE */
  158.     vp.DHeight = VPHEIGHT;
  159.     vp.RasInfo = &ri;
  160.     vp.Modes   = DUALPF;
  161.  
  162.     MakeVPort( &v, &vp );   /* construct copper instr (prelim) list */
  163.     MrgCop( &v );      /* merge prelim lists together into a real 
  164.                  * copper list in the view structure. */
  165.  
  166.     InitRastPort(&rp);   /* first playfield's rastport */
  167.     rp.BitMap = &b;      /* link to its bitmap */
  168.  
  169.     InitRastPort(&rp2);  /* second playfield's rastport */
  170.     rp2.BitMap = &b2;    /* link to its bitmap */
  171.  
  172.     SetRast(&rp,0);
  173.     SetRast(&rp2,0);
  174.     LoadView(&v);
  175.     setupjoystick ();
  176.  
  177.    file="board.pic";   //foreground gfx 1
  178.    readthepic();
  179.    BltBitMap(&b3,0,0,&b,0,0,WIDTH,HEIGHT,0xC0,0xff,NULL);
  180.  
  181.    file="about.pic";   //foreground gfx 2
  182.    readthepic();
  183.    BltBitMap(&b3,0,0,&b,0,200,WIDTH,HEIGHT,0xC0,0xff,NULL);
  184.  
  185.    file="tools.pic";  // spare page
  186.    readthepic();
  187.  
  188.     LOF[1] = NULL;   
  189.     SHF[1] = NULL;
  190.    do_init_game();
  191.    do_about(0);
  192.    do_about(1);
  193.    i=do_menu();
  194.    if (i==1)
  195.        game=1;
  196.    else
  197.    if (i==2)
  198.       game=2;
  199.    game_loop();
  200.     LoadView(oldview);       /* put back the old view  */
  201.     FreeMemory();            /* exit gracefully */
  202.     CloseLibrary(GfxBase);   /* since opened library, close it */
  203.  
  204. }   /* end of main() */
  205.  
  206.  
  207. /* return allocated memory */
  208. void FreeMemory()
  209. {
  210.    short i;
  211.  
  212.     /* free drawing area */
  213.     for(i=0; i<DEPTH; i++)
  214.    {
  215.         if (b.Planes[i]) FreeRaster(b.Planes[i],FWIDTH,FHEIGHT); 
  216.         if (b3.Planes[i]) FreeRaster(b3.Planes[i],WIDTH,HEIGHT); 
  217.         if (b2.Planes[i]) FreeRaster(b2.Planes[i],BWIDTH,BHEIGHT); 
  218.     }   
  219.  
  220.     /* free the color map created by GetColorMap() */
  221.     if (cm) FreeColorMap(cm);
  222.  
  223.     /* free dynamically created structures */
  224.     FreeVPortCopLists(&vp);         
  225.     FreeCprList(LOF[0]);
  226.     FreeCprList(LOF[1]);
  227. }   
  228.  
  229. void do_new_game()
  230. {
  231.         short i;
  232.       w=0;
  233.      
  234.      if (!page)
  235.      {
  236.        for (i=0; i < 200; i++)
  237.        {
  238.          swapPointers();
  239.          ri2.RxOffset = 0;
  240.          ri2.RyOffset = i;
  241.           remakeView();
  242.           w ^= 1;
  243.        }
  244.        page=1;
  245.      }
  246.      else
  247.      {
  248.        for (i=0; i < 200; i++)
  249.        {
  250.          swapPointers();
  251.          ri2.RxOffset = 0;
  252.          ri2.RyOffset = 199-i;
  253.           remakeView();
  254.           w ^= 1;
  255.        }
  256.        page=0;
  257.      }
  258.      SetRast(&rp2, 0);
  259. }  
  260.  
  261.  
  262. void swapPointers()      /* provided for double buffering of copper lists */
  263. {
  264.     LOF[w] = v.LOFCprList;
  265.     SHF[w] = v.SHFCprList;
  266.     v.LOFCprList = LOF[(w^1)];
  267.     v.SHFCprList = SHF[(w^1)];
  268.     /* swap the pointers so that they can reuse existing space */
  269. }
  270.  
  271. void remakeView()
  272. {
  273.     MakeVPort(&v, &vp);
  274.     MrgCop(&v);
  275.     LoadView(&v);      /* and show it */
  276.     WaitTOF();      /* slow things down so we can see it move */
  277. }
  278.  
  279. void setupjoystick()
  280. {
  281.   button_ptr=(char *)PA0; joy_ptr=(short *)JOY1DAT;
  282. }
  283.  
  284. void readjoystick()
  285. {
  286.   button_status=*button_ptr; joy_status=*joy_ptr;
  287.   joy_fire=((button_status & 128) >> 7) ^ 1;
  288.   joy_right=(joy_status & 2) >> 1;
  289.   joy_left=(joy_status & 512) >> 9;
  290.   joy_down=((joy_status & 2) >> 1) ^ (joy_status & 1);
  291.   joy_up=((joy_status & 512) >> 9) ^ ((joy_status & 256) >> 8);
  292. }
  293.  
  294. void readthepic()
  295. {
  296.  char *newbyte[8], code, goodbyte, counter;
  297.  short Height, Plane, bytes_read, bytes_per_row=WIDTH>>3;
  298.  FILE *IFFfile;
  299.  
  300.  IFFfile=fopen(file,"r");
  301.  for (Plane=0; Plane<DEPTH; Plane++)        
  302.  {
  303.     newbyte[Plane]=(char *)(b3.Planes[Plane]); 
  304.  }
  305.  for (Height=0; Height<HEIGHT; Height++)
  306.   { for (Plane=0; Plane<DEPTH; Plane++)
  307.     { bytes_read=0;
  308.       do
  309.       { fread(&code,1,1,IFFfile);
  310.         if (code>=0) 
  311.         { code++;
  312.           fread(newbyte[Plane],(long)code,1,IFFfile);
  313.           newbyte[Plane] += code;
  314.           bytes_read += code;
  315.         }
  316.         else if (code != -127) 
  317.          { code = -code;
  318.            code++;
  319.            fread(&goodbyte,1,1,IFFfile);
  320.            for (counter=0; counter<code; counter++)
  321.            { *newbyte[Plane]=goodbyte;
  322.               newbyte[Plane]++;
  323.            }
  324.            bytes_read += code;
  325.          }
  326.       }
  327.       while ((bytes_read<bytes_per_row) && feof(IFFfile)==0);
  328.     }
  329.   }
  330.  fclose(IFFfile);
  331. }
  332.  
  333. void read_qstick()
  334. {
  335.  readjoystick();
  336.  while (joy_fire || joy_left || joy_right || joy_up || joy_down)
  337.    readjoystick();
  338. }   
  339.  
  340. short do_menu()
  341. {
  342.  short menu=1;
  343.  
  344.  joy_fire=0;
  345.  SetAPen(&rp2,7);                 
  346.  RectFill(&rp2,247,34+offsety,316,45+offsety);
  347.  
  348.  read_qstick();
  349.  while (!joy_fire)
  350.  {
  351.      readjoystick();
  352.      if (joy_down && menu==1) //two player
  353.      {
  354.           SetAPen(&rp2,0);                 
  355.         RectFill(&rp2,247,34+offsety,316,45+offsety);
  356.  
  357.           SetAPen(&rp2,7);                 
  358.         RectFill(&rp2,247,52+offsety,316,63+offsety);
  359.        ++menu;
  360.      }
  361.      else
  362.      if (joy_down && menu==2)
  363.      {
  364.           SetAPen(&rp2,0);                 
  365.         RectFill(&rp2,247,52+offsety,316,63+offsety);
  366.  
  367.           SetAPen(&rp2,7);                 
  368.         RectFill(&rp2,247,70+offsety,316,81+offsety);
  369.        ++menu;
  370.      }
  371.      else
  372.      if (joy_down && menu==3)
  373.      {
  374.           SetAPen(&rp2,0);                 
  375.         RectFill(&rp2,247,70+offsety,316,81+offsety);
  376.  
  377.           SetAPen(&rp2,7);                 
  378.         RectFill(&rp2,247,89+offsety,316,100+offsety);
  379.        ++menu;
  380.      }
  381.      else
  382.      if (joy_down && menu==4)
  383.      {
  384.           SetAPen(&rp2,0);                 
  385.         RectFill(&rp2,247,89+offsety,316,100+offsety);
  386.  
  387.           SetAPen(&rp2,7);                 
  388.         RectFill(&rp2,247,108+offsety,316,119+offsety);
  389.        ++menu;
  390.      }
  391.      else
  392.      if (joy_down && menu==5)
  393.      {
  394.           SetAPen(&rp2,0);                 
  395.         RectFill(&rp2,247,108+offsety,316,119+offsety);
  396.  
  397.        SetAPen(&rp2,7);                 
  398.        RectFill(&rp2,247,34+offsety,316,45+offsety);
  399.        menu=1;
  400.      }
  401.      Delay(5);
  402.    }
  403.     SetAPen(&rp2,0);                 
  404.     RectFill(&rp2,247,30+offsety,316,130+offsety);
  405.    return(menu); 
  406. }
  407.  
  408. void move_peg(color, cord)
  409. short color, cord;
  410. {
  411.     SetAPen(&rp2,0);                 
  412.     RectFill(&rp2,10,10+offsety,240,40+offsety);
  413.     if (color==RED)
  414.        BltBitMap(&b3,13,156,&b2,drop[cord],14+offsety,22,15,0xC0,0xff,NULL);
  415.     else
  416.     if (color==BLUE)
  417.        BltBitMap(&b3,37,156,&b2,drop[cord],14+offsety,22,15,0xC0,0xff,NULL);
  418.     read_qstick();
  419. }
  420.  
  421. void game_loop()
  422. {
  423.  short done=0, cord=4, color=RED, valid=0, win;
  424.  
  425.  move_peg(color, cord);
  426.  while (!done)
  427.  {
  428.      readjoystick();
  429.      if (joy_fire && joy_down)  // end game
  430.        done=1;
  431.      else
  432.      if (joy_fire && joy_up)   // menu
  433.      {
  434.         done=do_menu();
  435.         if (done==1)
  436.         {
  437.            game=1;
  438.            done=0;
  439.         }
  440.         else
  441.         if (done==2)
  442.         {
  443.            game=2;
  444.            done=0;
  445.         }
  446.         else
  447.         if (done==5)    //about
  448.         {
  449.            do_about(0);
  450.            do_about(1);
  451.            done=0;
  452.         }
  453.         else
  454.         if (done==4)    // quit
  455.           done=1;
  456.         else
  457.         if (done==3)    //new game
  458.         {
  459.           do_new_game();
  460.           do_init_game();
  461.           read_qstick();
  462.           color=RED;
  463.           cord=4;
  464.           done=0;
  465.           if (offsety==0)
  466.              offsety=199;
  467.           else
  468.              offsety=0;
  469.           move_peg(color, cord);  
  470.         }
  471.         else
  472.           done=0;  
  473.         read_qstick();
  474.         joy_fire=joy_up=0;
  475.      }
  476.      else
  477.      if (joy_right)
  478.      {
  479.        ++cord;
  480.        if (cord > 8)
  481.          cord=0;
  482.        move_peg(color, cord);  
  483.      }
  484.      else
  485.      if (joy_left)
  486.      {
  487.        --cord;
  488.        if (cord < 0)
  489.          cord=8;
  490.        move_peg(color, cord);  
  491.      }
  492.      else
  493.      if (joy_fire && !joy_left && !joy_right && !joy_up && !joy_down)
  494.      {
  495.        valid=drop_peg(cord, color);
  496.        if (valid != -1)
  497.        {
  498.          --total_pegs;
  499.          win=check_win(color);      // check for a win
  500.          if (win !=-1)
  501.              do_game_over(win);
  502.          else
  503.          {
  504.             win=check_cross_win(color);
  505.             if (win !=-1)
  506.                do_game_over(win);
  507.          }
  508.  
  509.          if (total_pegs == 0)
  510.          {
  511.            do_game_over(TIE);
  512.            cord=4;
  513.          }
  514.          else
  515.          {
  516.            if (color==RED)
  517.              color=BLUE;
  518.            else
  519.              color=RED;
  520.            cord=4;
  521.            if (game==1 && color== BLUE)   // computer's mind
  522.            { 
  523.              win=comp_check_cross(3);
  524.              if (win != -1)
  525.                win=do_block_player(CROSS);
  526.              if (win == -1)
  527.              {
  528.                win=comp_check_down(3);
  529.                if (win != -1)
  530.                  win=do_block_player(DOWN);
  531.                else
  532.                {
  533.                  win=check_comp_rdiagonal(3);
  534.                  if (win != -1)
  535.                    win=do_block_player(RDIAGONAL);
  536.                  if (win==-1)
  537.                  {
  538.                    win=check_comp_ldiagonal(3);
  539.                    if (win != -1)
  540.                      win=do_block_player(LDIAGONAL);
  541.                  }
  542.                }
  543.              }               
  544.              
  545.              if (win==-1)
  546.              {
  547.                win=comp_check_cross(2);
  548.                if (win != -1)
  549.                  win=do_block_player(CROSS);
  550.                if (win == -1)
  551.                {
  552.                  win=comp_check_down(2);
  553.                  if (win != -1)
  554.                    win=do_block_player(DOWN);
  555.                }
  556.              }               
  557.  
  558.              if (win==-1)
  559.                do_random_move();
  560.              win=check_win(BLUE);      // check for computer win
  561.              if (win !=-1)
  562.                do_game_over(win);
  563.              else
  564.              {
  565.                win=check_cross_win(BLUE);
  566.                if (win !=-1)
  567.                  do_game_over(win);
  568.              }
  569.              cord=4;
  570.              color=RED;
  571.            }   
  572.          move_peg(color, cord);
  573.          }
  574.        }  
  575.      }
  576.   }
  577. }  
  578.  
  579. void do_random_move()
  580. {
  581.    short i, j, low=0, num_pegs[9];
  582.    
  583.    for (i=0; i < 9; i++)
  584.    {
  585.      num_pegs[i]=0;
  586.      for (j=0; j < 6; j++)
  587.        if (board[j][i] > 0)
  588.          ++num_pegs[i];
  589.    }
  590.  
  591.    j=0;
  592. pearl:
  593.    i=0;   
  594.    low=-1;
  595.    while (i < 9)
  596.    {
  597.       if (num_pegs[i++] == j)
  598.         low=i;
  599.    }
  600.    if (low == -1)
  601.    {
  602.      ++j;
  603.      if (j==9)
  604.        goto dead_game;
  605.      goto pearl;
  606.    }
  607.    --low;
  608.    move_peg(BLUE, low);  
  609.    i=drop_peg(low, BLUE);
  610.    goto next;
  611. dead_game:
  612.    do_game_over(TIE);
  613. next: ;
  614. }
  615.  
  616. short do_block_player(type)
  617. short type;
  618. {
  619.     short cord;
  620.     
  621.     if (type==CROSS)
  622.     {
  623.       if (board[Comp_stuff.firstx][Comp_stuff.firsty-1] == 0 &&
  624.           Comp_stuff.firsty-1 >= 0)
  625.          cord=Comp_stuff.firsty-1;
  626.       else
  627.       if (board[Comp_stuff.lastx][Comp_stuff.lasty+1] == 0 &&
  628.           Comp_stuff.lasty+1 < 9)
  629.          cord=Comp_stuff.lasty+1;
  630.       else
  631.          return(-1);
  632.     }
  633.     else
  634.     if (type==DOWN)
  635.     {
  636.       if (board[Comp_stuff.firstx-1][Comp_stuff.firsty] == 0 &&
  637.           Comp_stuff.firstx-1 >= 0)
  638.          cord=Comp_stuff.firsty;
  639.       else
  640.       if (board[Comp_stuff.lastx+1][Comp_stuff.lasty] == 0 && 
  641.           Comp_stuff.lastx+1 < 6)
  642.          cord=Comp_stuff.lasty;
  643.       else
  644.          return(-1);
  645.     }
  646.     else
  647.     if (type==RDIAGONAL)
  648.     {
  649.       if (board[Comp_stuff.firstx-1][Comp_stuff.firsty+1] == 0 &&
  650.           Comp_stuff.firstx-1 >= 0 &&
  651.           board[Comp_stuff.firstx-1][Comp_stuff.firsty+2] != 0)
  652.          cord=Comp_stuff.firsty+1;
  653.       else
  654.       if (board[Comp_stuff.lastx+1][Comp_stuff.lasty-1] == 0 && 
  655.           Comp_stuff.lastx+1 < 6 && Comp_stuff.lasty-1 >=0 &&
  656.           board[Comp_stuff.lastx+1][Comp_stuff.lasty-2] != 0)
  657.          cord=Comp_stuff.lasty-1;
  658.       else
  659.          return(-1);
  660.     }
  661.     else
  662.     if (type==LDIAGONAL)
  663.     {
  664.       if (board[Comp_stuff.firstx+1][Comp_stuff.firsty+1] == 0 &&
  665.           Comp_stuff.firstx+1 < 6 && Comp_stuff.firsty+1 <9)
  666.          cord=Comp_stuff.firsty+1;
  667.       else
  668.       if (board[Comp_stuff.lastx-1][Comp_stuff.lasty-1] == 0 && 
  669.           Comp_stuff.lastx-1 >= 0 && Comp_stuff.lasty-1 >= 0)
  670.          cord=Comp_stuff.lasty-1;
  671.       else
  672.          return(-1);
  673.     }
  674.     
  675.     move_peg(BLUE, cord);  
  676.     cord=drop_peg(cord, BLUE);
  677.     return(0);
  678. }    
  679.  
  680. void do_init_game()
  681. {
  682.    register short i, j;
  683.    
  684.    total_pegs=54;
  685.    SetRast(&rp2, 0);
  686.    for (i=0; i<8; i++)
  687.      for (j=0; j<11; j++)
  688.        board[i][j]=0;
  689. }
  690.  
  691. short drop_peg(cord, color)
  692. short cord, color;
  693. {
  694.    short i, box;
  695.    
  696.    box=get_box(cord, color);
  697.    read_qstick();
  698.    if (box == -1)
  699.      return(-1);
  700.    for (i=0; i< dropy[box]; i++)
  701.       ScrollRaster(&rp2, 0, -1, drop[cord], 0+offsety, drop[cord]+25,
  702.        dropy[box]+30+offsety);
  703.    return(0);
  704. }
  705.  
  706. short get_box(cord, color)
  707. short cord, color;
  708. {
  709.    register short i;
  710.    
  711.    for (i=5; i > -1; i--)
  712.      if (board[i][cord] == 0)
  713.      {
  714.        board[i][cord]=color;
  715.        return(i);
  716.      }
  717.    return(-1);
  718. }
  719.    
  720. void do_about(page)
  721. short page;
  722. {
  723.         short i;
  724.       w=0;
  725.      
  726.      if (!page)
  727.      {
  728.        for (i=0; i < 200; i++)
  729.        {
  730.          swapPointers();
  731.          ri.RxOffset = 0;
  732.          ri.RyOffset = i;
  733.           remakeView();
  734.           w ^= 1;
  735.        }
  736.        read_qstick();
  737.        while (!joy_fire)
  738.           readjoystick();
  739.      }
  740.      else
  741.      {
  742.        for (i=0; i < 200; i++)
  743.        {
  744.          swapPointers();
  745.          ri.RxOffset = 0;
  746.          ri.RyOffset = 199-i;
  747.           remakeView();
  748.           w ^= 1;
  749.        }
  750.      }
  751. }  
  752.  
  753. void do_game_over(type)
  754. short type;
  755. {
  756.    short i;
  757.    
  758.    read_qstick();
  759.    if (type==TIE)
  760.    {
  761.       SetAPen(&rp, 7);
  762.       Move(&rp, 250, 138);
  763.       Text(&rp, "Tie Game", 8);
  764.    } 
  765.    else
  766.    if (type==RED)
  767.    {
  768.       SetAPen(&rp, 5);
  769.       Move(&rp, 250, 138);
  770.       Text(&rp, "Red  Win", 8);
  771.    } 
  772.    else
  773.    if (type==BLUE)
  774.    {
  775.       SetAPen(&rp, 4);
  776.       Move(&rp, 250, 138);
  777.       Text(&rp, "Blue Win", 8);
  778.    } 
  779.  
  780.    SetAPen(&rp2, 6);
  781.    Move(&rp2, 250, 170+offsety);
  782.    Text(&rp2, "Hit Fire", 8);
  783.  
  784.    joy_fire=0;
  785.    while (!joy_fire)
  786.    {
  787.      readjoystick();
  788.      i=0;
  789.      while (!joy_fire && i++ < 31)
  790.      {
  791.       readjoystick();
  792.       ScrollRaster(&rp,  0, -1, 248, 130, 315, 170);
  793.       ScrollRaster(&rp2, 0,  1, 248, 130+offsety, 315, 170+offsety);
  794.       Delay(1);
  795.      }
  796.      i=0;
  797.      while (!joy_fire && i++ < 31)
  798.      {
  799.       readjoystick();
  800.       ScrollRaster(&rp,  0,  1, 248, 130, 315, 170);
  801.       ScrollRaster(&rp2, 0, -1, 248, 130+offsety, 315, 170+offsety);
  802.       Delay(1);
  803.      }
  804.    }
  805.    SetAPen(&rp2,0);                 
  806.     RectFill(&rp2,247,130+offsety,316,170+offsety);
  807.    SetAPen(&rp,0);                 
  808.     RectFill(&rp,247,130,316,170);
  809.    read_qstick();
  810.    do_new_game();
  811.    do_init_game();
  812.    if (offsety==0)
  813.         offsety=199;
  814.    else
  815.         offsety=0;
  816.    move_peg(RED, 4);  
  817. }
  818.  
  819. short check_win(player)
  820. short player;
  821. {
  822.    short i,j, count;
  823.    
  824.    for (i=0;i<6;i++)   // cross checking
  825.    {
  826.      count=0;
  827.      for (j=0;j<9;j++)
  828.      {
  829.        if (board[i][j]==player)
  830.           ++count;
  831.        else
  832.           count=0;
  833.  
  834.        if (count==4)
  835.         return(player);
  836.      }
  837.    }
  838.  
  839.    for (i=0;i<9;i++)   // down checking
  840.    {
  841.      count=0;
  842.      for (j=0;j<6;j++)
  843.      {
  844.        if (board[j][i]==player)
  845.           ++count;
  846.        else
  847.           count=0;
  848.        if (count==4)
  849.         return(player);         
  850.      }
  851.    }
  852.    return(-1);
  853. }
  854.  
  855. void print_board()
  856. {
  857.    short i, j;
  858.       
  859.    for (i=0;i<6;i++)
  860.    {
  861.      for (j=0; j<9;j++)
  862.        printf("%d ", board[i][j]);
  863.      printf("\n");
  864.    }
  865. }  
  866.    
  867. short check_cross_win(player)
  868. short player;
  869. {
  870.    short i,j, count, temp;
  871.    
  872.    for (i=4; i < 12; i++)  //  '/' right hatch check
  873.    {
  874.       count=0;
  875.       temp=i;
  876.       for (j=-1; j < 7; j++)
  877.       {
  878.          if (board[j+1][temp-1] == player)
  879.            ++count;
  880.          else
  881.            count=0;
  882.          --temp;
  883.          if (count==4)
  884.            return(player);
  885.       }
  886.     }
  887.  
  888.    for (i=4; i < 12; i++)  // '\' left hatch check
  889.    {
  890.       count=0;
  891.       temp=i;
  892.       for (j=6; j > -1; j--)
  893.       {
  894.          if (board[j-1][temp-1] == player)
  895.            ++count;
  896.          else
  897.            count=0;
  898.          --temp;
  899.          if (count==4)
  900.            return(player);
  901.       }
  902.     }
  903.     return(-1);
  904. }  
  905.  
  906. short comp_check_cross(num)  // num is the peg count
  907. short num;
  908. {
  909.    short count, i, j, first=0;
  910.    
  911.    for (i=0;i<6;i++)   // cross checking
  912.    {
  913.      count=first=0;
  914.      for (j=0;j<9;j++)
  915.      {
  916.        if (board[i][j]==RED)
  917.        {
  918.           ++count;
  919.           if (!first)
  920.           {
  921.              Comp_stuff.firstx=i;
  922.              Comp_stuff.firsty=j;
  923.              first=1;
  924.           }
  925.           else
  926.           {
  927.             Comp_stuff.lastx=i;
  928.             Comp_stuff.lasty=j;
  929.           }
  930.           if (count==num)
  931.              return(num);
  932.        }
  933.        else
  934.          count=0;
  935.      }
  936.    }
  937.    return(-1);
  938. }
  939.  
  940. short comp_check_down(num)  // num is the peg count
  941. short num;
  942. {
  943.    short i, j, count, first;
  944.    
  945.    for (i=0;i<9;i++)   // down checking
  946.    {
  947.      count=first=0;
  948.      for (j=0;j<6;j++)
  949.      {
  950.        if (board[j][i]==RED)
  951.        {
  952.           ++count;
  953.           if (!first)
  954.           {
  955.              Comp_stuff.firstx=j;
  956.              Comp_stuff.firsty=i;
  957.              first=1;
  958.           }
  959.           else
  960.           {
  961.             Comp_stuff.lastx=j;
  962.             Comp_stuff.lasty=i;
  963.           }
  964.           if (count==num)
  965.             return(num);         
  966.        }
  967.        else
  968.          count=0;
  969.     }
  970.    }
  971.    return(-1);
  972. }
  973.  
  974.  
  975. short check_comp_rdiagonal(num)
  976. short num;
  977. {
  978.    short i, j, count, first=0, temp;
  979.    
  980.    for (i=4; i < 12; i++)  //  '/' right hatch check
  981.    {
  982.       count=0;
  983.       temp=i;
  984.       for (j=-1; j < 7; j++)
  985.       {
  986.          if (board[j+1][temp-1] == RED)
  987.          {
  988.            ++count;
  989.            if (!first)
  990.            {
  991.              Comp_stuff.firstx=j+1;
  992.              Comp_stuff.firsty=temp-1;
  993.              first=1;
  994.            }
  995.            else
  996.            {
  997.              Comp_stuff.lastx=j+1;
  998.              Comp_stuff.lasty=temp-1;
  999.            }
  1000.            if (count==num)
  1001.              return(num);         
  1002.          }
  1003.          else    
  1004.            count=0;
  1005.          --temp;
  1006.       }
  1007.    }
  1008.    return(-1);
  1009. }
  1010.  
  1011. short check_comp_ldiagonal(num)
  1012. short num;
  1013. {
  1014.    short i, j, count, first=0, temp;
  1015.    
  1016.    for (i=4; i < 12; i++)  // '\' left hatch check
  1017.    {
  1018.       count=0;
  1019.       temp=i;
  1020.       for (j=6; j > -1; j--)
  1021.       {
  1022.          if (board[j-1][temp-1] == RED)
  1023.          {
  1024.            ++count;
  1025.            if (!first)
  1026.            {
  1027.              Comp_stuff.firstx=j-1;
  1028.              Comp_stuff.firsty=temp-1;
  1029.              first=1;
  1030.            }
  1031.            else
  1032.            {
  1033.              Comp_stuff.lastx=j-1;
  1034.              Comp_stuff.lasty=temp-1;
  1035.            }
  1036.            if (count==num)
  1037.              return(num);         
  1038.          }
  1039.          else    
  1040.            count=0;
  1041.          --temp;
  1042.       }
  1043.    }
  1044.    return(-1);
  1045. }
  1046.  
  1047.